package com.javasm.finance.controller;

import com.javasm.finance.entity.CodeAndMsg;
import com.javasm.finance.entity.PageInfo;
import com.javasm.finance.entity.ReturnEntity;
import com.javasm.finance.entity.User;
import com.javasm.finance.service.UserService;
import com.javasm.finance.service.impl.UserServiceImpl;
import com.javasm.finance.util.RespUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/user/*")
public class UserController extends BaseController{
    UserService userService=new UserServiceImpl();

    public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收请求数据
        //当前页
        String pageStr = req.getParameter("page");
        Integer page = 1;
        if (pageStr != null && !"".equals(pageStr)) {
            page = Integer.valueOf(pageStr);
        }
        //每页显示的数据条数
        String pageSizeStr = req.getParameter("pageSize");
        Integer pageSize = 5;
        if (pageSizeStr != null && !"".equals(pageSizeStr)) {
            pageSize = Integer.valueOf(pageSizeStr);
        }
        //用户编号
        String userIdStr = req.getParameter("userId");
        Integer userId = null;
        if (userIdStr != null && !"".equals(userIdStr)) {
            userId = Integer.valueOf(userIdStr);
        }
        //用户名称
        String userName = req.getParameter("userName");
        User user = new User(userId, userName);
        //调用方法
        List<User> userList = userService.findUser(page, pageSize, user);
        //设置响应数据
        ReturnEntity re = new ReturnEntity();
        if (userList != null && userList.size() > 0) {
            //查询数据总条数
            long totalNum = userService.findTotalNum(user);
            PageInfo pageInfo = new PageInfo(page, pageSize, totalNum);
            //创建Map集合
            Map<String, Object> returnMap = new HashMap<>();
            returnMap.put("userList", userList);
            returnMap.put("pageInfo", pageInfo);
            re.setReturnCode(CodeAndMsg.QUERY_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_SUCCESS.getReturnMsg());
            re.setReturnData(returnMap);
        } else {
            //创建Map集合
            Map<String, Object> returnMap = new HashMap<>();
            returnMap.put("pageInfo", new PageInfo(1, 5, 0l));
            re.setReturnCode(CodeAndMsg.QUERY_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_FAILED.getReturnMsg());
            re.setReturnData(returnMap);
        }
        //写出响应数据
        RespUtils.handleResp(resp, re);
    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收请求数据
        //用户名称
        String userName = req.getParameter("userName");
        //人员状态
        String isValidStr = req.getParameter("isValid");
        Integer isValid = null;
        if (isValidStr != null && !"".equals(isValidStr)) {
            isValid = Integer.valueOf(isValidStr);
        }
        //人员角色
        String roleIdStr = req.getParameter("roleId");
        Integer roleId = null;
        if (roleIdStr != null && !"".equals(roleIdStr)) {
            roleId = Integer.valueOf(roleIdStr);
        }
        String regTime = req.getParameter("regTime");
        //用户头像
        String headImg = req.getParameter("headImg");
        User user = new User(userName, roleId, regTime,isValid, headImg);
        //调用方法
        int rows = userService.addUser(user);
        //设置响应数据
        ReturnEntity re = new ReturnEntity();
        if (rows > 0) {
            re.setReturnCode(CodeAndMsg.OPERATE_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_SUCCESS.getReturnMsg());
        } else {
            re.setReturnCode(CodeAndMsg.OPERATE_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_FAILED.getReturnMsg());
        }
        //写出响应数据
        RespUtils.handleResp(resp, re);
    }

    public void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //用户编号
        String userIdStr = req.getParameter("userId");
        Integer userId = null;
        if (userIdStr != null && !"".equals(userIdStr)) {
            userId = Integer.valueOf(userIdStr);
        }
        //查询用户数据
        User user = userService.findByUid(userId);
        //设置响应数据
        ReturnEntity re = new ReturnEntity();
        if (user != null) {
            re.setReturnCode(CodeAndMsg.QUERY_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_SUCCESS.getReturnMsg());
            re.setReturnData(user);
        } else {
            re.setReturnCode(CodeAndMsg.QUERY_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_FAILED.getReturnMsg());
        }
        //写出响应数据
        RespUtils.handleResp(resp, re);
    }

    public void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //接收请求数据
        //用户编号
        String userIdStr = req.getParameter("userId");
        Integer userId = null;
        if (userIdStr != null && !"".equals(userIdStr)) {
            userId = Integer.valueOf(userIdStr);
        }
        //用户名称
        String userName = req.getParameter("userName");
        //人员状态
        String isValidStr = req.getParameter("isValid");
        Integer isValid = null;
        if (isValidStr != null && !"".equals(isValidStr)) {
            isValid = Integer.valueOf(isValidStr);
        }
        //人员角色
        String roleIdStr = req.getParameter("roleId");
        Integer roleId = null;
        if (roleIdStr != null && !"".equals(roleIdStr)) {
            roleId = Integer.valueOf(roleIdStr);
        }
        //注册时间
        String regTime = req.getParameter("regTime");
        //用户头像
        String headImg = req.getParameter("headImg");
        User user = new User(userId, userName, roleId, regTime, isValid, headImg);
        //调用方法
        int rows = userService.editUser(user);
        //设置响应数据
        ReturnEntity re = new ReturnEntity();
        if (rows > 0) {
            re.setReturnCode(CodeAndMsg.OPERATE_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_SUCCESS.getReturnMsg());
        } else {
            re.setReturnCode(CodeAndMsg.OPERATE_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_FAILED.getReturnMsg());
        }
        //写出响应数据
        RespUtils.handleResp(resp, re);
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //接收请求数据
        //用户编号
        String userIdStr = req.getParameter("userId");
        Integer userId = null;
        if (userIdStr != null && !"".equals(userIdStr)) {
            userId = Integer.valueOf(userIdStr);
        }
        //调用方法
        boolean isSuccess = userService.deleteUser(userId);
        //设置响应数据
        ReturnEntity re = new ReturnEntity();
        if (isSuccess) {
            re.setReturnCode(CodeAndMsg.OPERATE_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_SUCCESS.getReturnMsg());
        } else {
            re.setReturnCode(CodeAndMsg.OPERATE_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_FAILED.getReturnMsg());
        }
        //写出响应数据
        RespUtils.handleResp(resp, re);
    }
}
